package cs9322.assign3.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.mysql.jdbc.Statement;
import cs9322.assign3.model.Order;
import cs9322.assign3.model.Payment;

public class PaymentDao {
	Connection connection = null;
	PreparedStatement ptmt = null;
	ResultSet resultSet = null;
	
	public PaymentDao() {
	}

	private Connection getConnection() throws SQLException {
		Connection conn;
		conn = ConnectionFactory.getInstance().getConnection();
		return conn;
	}
	
	public void add(Payment pay) {
        try {
                String queryString = "INSERT INTO payment(orderId, type, amount, url,detail) VALUES(?,?,?,?,?)";
                connection = getConnection();
                ptmt = connection.prepareStatement(queryString);
                ptmt.setInt(1, pay.getOrderId());
                ptmt.setString(2, pay.getPaymentType());
                ptmt.setDouble(3, pay.getPaymentAmount());
                ptmt.setString(4, pay.getUrl());
                ptmt.setString(5, pay.getPaymentDetail());
                ptmt.executeUpdate();
                System.out.println("Payment  Added Successfully");
        } catch (SQLException e) {
                e.printStackTrace();
        } finally {
                try {
                        if (ptmt != null)
                                ptmt.close();
                        if (connection != null)
                                connection.close();
                } catch (SQLException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }

        }
	}
        public void update(Payment pay2) {

    		try {
    			String queryString = "UPDATE payment SET type=?, amount=?, url=?, detail=? where orderId=?";
    			connection = getConnection();
    			ptmt = connection.prepareStatement(queryString);
    			ptmt.setString(1, pay2.getPaymentType());
    			ptmt.setDouble(2, pay2.getPaymentAmount());
    			ptmt.setString(3, pay2.getUrl());
    			ptmt.setString(4, pay2.getPaymentDetail());
    			ptmt.setInt(5, pay2.getOrderId());
    			ptmt.executeUpdate();
    			System.out.println("payment Updated Successfully");
    		} catch (SQLException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (ptmt != null)
    					ptmt.close();
    				if (connection != null)
    					connection.close();
    			}

    			catch (SQLException e) {
    				e.printStackTrace();
    			} catch (Exception e) {
    				e.printStackTrace();

    			}
    		}
    	}
        
        public ArrayList<Payment> getAllPayments() {
    		ArrayList<Payment> result = new ArrayList<Payment>();
    		Payment p = null;
    		try {
    			String queryString = "select * from payment";
    			connection = getConnection();
    			ptmt = connection.prepareStatement(queryString);
    			resultSet = ptmt.executeQuery();

    			while (resultSet.next()) {
    				p = new Payment();
    				p.setOrderId(resultSet.getInt("orderId"));
    				p.setPaymentAmount(resultSet.getDouble("amount"));
    				p.setPaymentDetail(resultSet.getString("detail"));
    				p.setPaymentType(resultSet.getString("type"));
    				p.setUrl(resultSet.getString("url"));
    				result.add(p);
    			}
    			return result;
    		} catch (SQLException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (ptmt != null)
    					ptmt.close();
    				if (connection != null)
    					connection.close();
    			}

    			catch (SQLException e) {
    				e.printStackTrace();
    			} catch (Exception e) {
    				e.printStackTrace();

    			}
    		}
    		return result;
    	}
        
        public Payment findByOrderId(int id) {
    		Payment p = null;
    		try {
    			String queryString = "select * from payment where orderId=?";
    			connection = getConnection();
    			ptmt = connection.prepareStatement(queryString);
    			ptmt.setInt(1, id);
    			resultSet = ptmt.executeQuery();

    			while (resultSet.next()) {
    				p = new Payment();
    				p.setOrderId(id);
    				p.setPaymentAmount(resultSet.getDouble("amount"));
    				p.setPaymentDetail(resultSet.getString("detail"));
    				p.setPaymentType(resultSet.getString("type"));
    				p.setUrl(resultSet.getString("url"));
    				
//    				o.setId(resultSet.getInt("idorder"));
//    				o.setType(resultSet.getString("type"));
//    				o.setSize(resultSet.getString("size"));
//    				o.setPaid(resultSet.getBoolean("paid"));
//    				o.setAddition(resultSet.getString("addition"));
//    				o.setPrice(resultSet.getDouble("price"));
//    				o.setStatus(resultSet.getString("status"));
//    				o.setUrl(resultSet.getString("url"));
    			}
    			return p;

    		} catch (SQLException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (ptmt != null)
    					ptmt.close();
    				if (connection != null)
    					connection.close();
    			}

    			catch (SQLException e) {
    				e.printStackTrace();
    			} catch (Exception e) {
    				e.printStackTrace();

    			}
    		}
    		return p;
    	}
}
